<!--
 * @Date: 2025-01-19 11:51:00
 * @LastEditors: zl 1077167261@qq.com
 * @LastEditTime: 2025-01-19 17:55:40
 * @FilePath: \vue-admin\src\views\Login\index.vue
 用户名: demo
密码:  Hmzs%001
-->
<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form :model="formData" :rules="rules" ref="form">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password" class="login_input">
          <el-input
            v-if="IconName === 'preview'"
            v-model="formData.password"
            type="password"
          />
          <el-input v-else v-model="formData.password" />
          <svg-icon :icon-class="IconName" class="icon" @click="changeIcon" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember" @change="saveData"
            >记住我</el-checkbox
          >
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
const FORMDATA_KEY = "form_key";
export default {
  name: "Login",
  mounted() {
    const cacheFormStr = localStorage.getItem(FORMDATA_KEY);
    if (cacheFormStr) {
      const cacheFormData = JSON.parse(cacheFormStr);
      this.formData.username = cacheFormData.username;
      this.formData.password = cacheFormData.password;
    }
  },
  data() {
    return {
      IconName: "preview",
      formData: {
        username: "",
        password: "",
        remember: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入账号",
            trigger: ["blur", "change"],
          },
          {
            min: 4,
            max: 11,
            message: "长度在 4 到 11 个字符",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^.{5,10}$/,
            message: "请输入 5 到 10 位的密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    saveData() {},
    // 修改图标
    changeIcon() {
      this.IconName = this.IconName === "preview" ? "hidden" : "preview";
    },
    //-----------------------------------------------------1登录
    login() {
      this.$refs.form.validate(async (valid) => {
        console.log(valid);
        if (valid) {
          // 登录+记住我功能实现
          console.log("登录");
          const { username, password, remember } = this.formData;
          await this.$store.dispatch("user/doLogin", { username, password });
          this.$router.push("/");

          // 补充remeber逻辑
          if (remember) {
            localStorage.setItem(
              FORMDATA_KEY,
              JSON.stringify({ username, password })
            );
          } else {
            localStorage.removeItem(FORMDATA_KEY);
          }
        }
      });
    },
  },
};
</script>
1

<style scoped lang="scss">
.login_body {
  display: flex;
}
.bg {
  width: 50vw;
  height: 100vh;
  background: url("~@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}
.box {
  margin: 20% 10%;
  flex: 1;
  text-align: center;
  box-shadow: 2px 2px 10px #ccc;
  padding: 20px;
  user-select: none;
  .login_input {
    position: relative;
    .icon {
      position: absolute;
      top: 48px;
      right: 1vw;
      font-size: 24px;
    }
  }

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }
    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }
    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}
.login_btn {
  width: 100%;
}
</style>
